﻿Shader "Shadow/ShadowMap"
{
    Properties
    {
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }

        Pass
        {
			Cull Off ZWrite On ZTest LEqual

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
				float3 normal : NORMAL;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
				float2 depth : TEXCOORD0;
            };

			uniform float _gShadowBias;
			uniform float _gShadowNormalBias;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = mul(unity_ObjectToWorld, v.vertex);
				float3 normal = normalize(mul(v.normal, unity_WorldToObject));
				//o.vertex.xyz -= normal * _gShadowNormalBias;
				o.vertex = UnityWorldToClipPos(o.vertex);
#if defined(SHADER_TARGET_GLEL)
				o.vertex.z += _gShadowBias * 2;
#elif defined(UNITY_REVERSED_Z)
				o.vertex.z -= _gShadowBias;
#else
				o.vertex.z += _gShadowBias;
#endif
				o.depth = o.vertex.zw;
                return o;
            }

            float4 frag (v2f i) : SV_Target
            {
				float depth = i.depth.x / i.depth.y;
#if defined(SHADER_TARGET_GLEL)
				depth = depth * 0.5 + 0.5;
#elif defined(UNITY_REVERSED_Z)
				depth = 1 - depth;
#endif
				return depth;

                //return EncodeFloatRGBA(depth);
            }
            ENDCG
        }
    }
}
